import {KEYS} from './keys'

function setStorageData(storageKey: KEYS, value: any): void {
  try {
    localStorage.setItem(storageKey, JSON.stringify(value))
  } catch (e) {
    console.log('setStorageData:', e)
  }
}

function getStorageData(storageKey: KEYS, defaultValue?: any): any {
  try {
    const value: string = localStorage.getItem(storageKey)
    if (value) {
      return JSON.parse(value)
    } else {
      return defaultValue ? defaultValue : null
    }
  } catch (e) {
    console.log(e)
  }
}

function clearData(): void {
  try {
    localStorage.clear()
  } catch (e) {
    console.log(e)
  }
}

function removeItem(storageKey: KEYS): void {
  try {
    localStorage.removeItem(storageKey)
  } catch (e) {
    console.log(e)
  }
}

export {setStorageData, getStorageData, clearData, removeItem}
